import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '@/views/Home.vue'
import AntigenHome from "@/views/antigen/AntigenHome.vue";
import IndividualHome from "@/views/individual/IndividualHome.vue";
import UploadAntigenMsg from "@/views/antigen/UploadAntigenMsg.vue";
import UploadAntigenMsgForFmly from "@/views/antigen/UploadAntigenMsgForFmly.vue";
import AntigenResult from "@/views/antigen/AntigenResult.vue";
import DetectionRecord from "@/views/individual/DetectionRecord.vue";
import Setting from "@/views/individual/Setting.vue";
import Register from "@/views/Register.vue";
import SetMessage from "@/views/SetMessage.vue";
import AntigenResultDetail from "@/views/antigen/AntigenResultDetail";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login
    },
	{
		path: '/setMessage',
		name: 'SetMessage',
		component: SetMessage,
		meta: {
			requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
		}
	},
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
		meta: {
			requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
		},
        children: [
            {
                path: '/antigenHome',
                name: 'AntigenHome',
                component: AntigenHome,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/individualHome',
                name: 'IndividualHome',
                component: IndividualHome,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/uploadAntigenMsg',
                name: 'UploadAntigenMsg',
                component: UploadAntigenMsg,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/uploadAntigenMsgForFmly',
                name: 'UploadAntigenMsgForFmly',
                component: UploadAntigenMsgForFmly,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/antigenResult',
                name: 'AntigenResult',
                component: AntigenResult,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/antigenResultDetail',
                name: 'AntigenResultDetail',
                component: AntigenResultDetail,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/detectionRecord',
                name: 'DetectionRecord',
                component: DetectionRecord,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },
            {
                path: '/setting',
                name: 'Setting',
                component: Setting,
                meta: {
                    requireAuth: true, // true表示需要检查是否登录，登录后才可以访问改路径
                }
            },

        ]
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 登录拦截（登录和注册页不用拦截）
router.beforeEach((to, from, next) => {
	var flag = localStorage.getItem("isLogin");
	// num -> null:未登录，1:已登录
	if (to.meta.requireAuth && flag == null) { // 判断该路由是否需要登录权限
		console.log("执行登录拦截")
		next({
			path: '/', //返回登录界面
			// query: {redirect: to.fullPath}
		})
	} else {
		//如果不需要登录权限或已经登录就直接跳转到该路由
		next();
	}
})

export default router
